/*
 *  $Id: sysapi_link.c,v 1.4 2006-06-09 06:17:24 bacon Exp $
 */

#include <xp/bas/sysapi.h>
#include <xp/bas/locale.h>
#include <xp/bas/errno.h>

int xp_link (const xp_char_t* name1, const xp_char_t* name2)
{
#if defined(_WIN32)
	return -1;
#elif defined(XP_CHAR_IS_MCHAR)
	return link (name1, name2);
#else
	xp_mchar_t name1_mb[XP_PATH_MAX + 1];
	xp_mchar_t name2_mb[XP_PATH_MAX + 1];

	if (xp_wcstomcs_strict(name1, name1_mb, xp_countof(name1_mb)) == -1) {
		return -1;
	}

	if (xp_wcstomcs_strict(name2, name2_mb, xp_countof(name2_mb)) == -1) {
		return -1;
	}

	return link (name1_mb, name2_mb);
#endif
}

int xp_symlink (const xp_char_t* name1, const xp_char_t* name2)
{
#if defined(_WIN32)
	return -1;
#elif defined(XP_CHAR_IS_MCHAR)
	return symlink (name1, name2);
#else
	xp_mchar_t name1_mb[XP_PATH_MAX + 1];
	xp_mchar_t name2_mb[XP_PATH_MAX + 1];

	if (xp_wcstomcs_strict(name1, name1_mb, xp_countof(name1_mb)) == -1) {
		return -1;
	}

	if (xp_wcstomcs_strict(name2, name2_mb, xp_countof(name2_mb)) == -1) {
		return -1;
	}

	return symlink (name1_mb, name2_mb);
#endif
}

int xp_readlink (const xp_char_t* path, xp_char_t* buf, xp_size_t size)
{
#if defined(_WIN32)
	return -1;
#elif defined(XP_CHAR_IS_MCHAR)
	return readlink (path, buf, size);
#else
	xp_mchar_t path_mb[XP_PATH_MAX + 1];
	xp_mchar_t buf_mb[XP_PATH_MAX + 1];

	if (xp_wcstomcs_strict(path, path_mb, xp_countof(path_mb)) == -1) {
		return -1;
	}

	if (readlink (path_mb, buf_mb, xp_countof(buf_mb)) == -1) return -1;

	if (xp_mcstowcs_strict(buf_mb, buf, size) == -1) {
		return -1;
	}
	
	return 0;
#endif
}

